import React from 'react';
import {CtripEditFrame, DidiEditFrame, MeiyaEditFrame, YgetEditFrame, HeliosHotelFrame, HeliosPlaneFrame,TravelNoOneEditFrame} from "./edit-frames";
import CtripSetting from 'containers/supplier-management/new-consumer-management/settings/ctrip-setting';
import ConsumerOpenDetail from './consumer-open-detail';
import CMBCOpenDetail from './detail/cmbc-open-detail';
import CardOpenDetail from './detail/card-open-detail';
import AlipayOpenDetail from './detail/alipay-open-detail';
import GaodeOpenDetail from './detail/gaode-open-detail';
import ConsumerOtherSetting from './consumer-other-setting';
import CMBCSetting from './settings/cmbc-setting';
import YgetQutoAdjust from '../overview/supplier-detail/quota-adjust/yget-quota-adjust.js'
import AdvanceTable from "components/advanced-table";
import {messages} from "share/common";
import {Form,Checkbox, Radio, Row, Col,} from 'antd';
import ConsumerUtil2 from './consumer-util2';
const FormItem = Form.Item;

export default {

  initPriorityData(props){
    const {params, tenant} = props;
    let isEdit = params.levelCode ? true : false;
    const initValue = {
      levelCode: isEdit ? params.levelCode : "TENANT",
      levelOrgId: isEdit ? params.levelOrgId : tenant.id,
      levelOrgName: isEdit ? params.levelOrgName || (params.levelCode === 'COMPANY' ? params.distLevelOrgName : '') : tenant.tenantName,
    }
    return initValue;
  },

  getCtripKeys(type, editData){
    let options = [
      { label: messages('supplier-1.key80')/*机票*/, value: false },
      { label: messages('supplier-1.key81')/*酒店*/, value: false },
      { label: messages('supplier-1.key82')/*火车*/, value: false },
      { label: messages('supplier-1.key83')/*用车*/, value: false },
    ];
    let orderOptions = [
      { label: messages('supplier-1.key84')/*公务舱*/, value: false },
      { label: messages('supplier-1.key85')/*头等舱*/, value: false },
      { label: messages('supplier-1.key86')/*经济舱*/, value: false },
      { label: messages('supplier-1.key87')/*超级经济舱*/, value: false },
    ];
    let tickesOptions = [
      { label: messages('supplier-1.key88')/*国内机票*/, value: false },
      { label: messages('supplier-1.key89')/*国际机票*/, value: false },
      { label: messages('supplier-1.key81')/*酒店*/, value: false },
      { label: messages('supplier-1.key83')/*用车*/, value: false },
      { label: messages('supplier-1.key90')/*火车票*/, value: false },
    ];
    let beforeOptionsAir = [
      { label: messages('common.date')/*日期*/, value: false },
      { label: messages('supplier-1.key91')/*出行人*/, value: false },
      { label: messages('supplier-1.key92')/*出发、到达城市*/, value: false },
      { label: messages('supplier-1.key93')/*舱等*/, value: false },
    ];
    let beforeOptionsHotel = [
      { label: messages('common.date')/*日期*/, value: false },
      { label: messages('supplier-1.key94')/*入住人*/, value: false },
      { label: messages('supplier-1.key95')/*入住城市*/, value: false },
    ];
    let keys = [];
    switch (type){
      case 'Supplier':// 应用类型
        options = options.concat([{ label: messages('supplier-1.key96')/*授权*/, value: false },]);
        keys = ['withFlightSupplier','withHotelSupplier','withTrainSupplier','withCarSupplier','withAccreditSupplier'];
        return options.map((item, index) => {
          item.keyName = keys[index];
          item.value = editData[keys[index]] || false;
          return item;
        });
      case 'Approval':// 行程上可选该服务商
        keys = ['withFlightApproval','withHotelApproval','withTrainApproval'/*,'withCarApproval'*/];
        options.splice(options.length-1,1);
        return options.map((item, index) => {
          item.keyName = keys[index];
          item.value = editData[keys[index]] || false;
          return item;
        });
      case 'PCSso':// 服务商预订首页
        keys = ['withFlightPCSso','withHotelPCSso','withTrainPCSso','withCarPCSso'];
        return options.map((item, index) => {
          item.keyName = keys[index];
          item.value = editData[keys[index]] || false;
          return item;
        });
      case 'PCOrder':// 服务商订单列表
        keys = ['withFlightPCOrder','withHotelPCOrder','withTrainPCOrder','withCarPCOrder'];
        return options.map((item, index) => {
          item.keyName = keys[index];
          item.value = editData[keys[index]] || false;
          return item;
        });
      case 'Class':// {messages('supplier-1.key97')/*国际机票舱位屏蔽*/}
        keys = ['intlCClass','intlFClass','intlYClass','intlSYClass'];
        return orderOptions.map((item, index) => {
          item.keyName = keys[index];
          item.value = editData[keys[index]] || false;
          return item;
        });
      case 'TRFlag':
        keys = ['useAirTRFlag','useIntlAirTRFlag','useHotelTRFlag','useCarTRFlag', 'useTrainTRFlag'];
        return tickesOptions.map((item, index) => {
          item.keyName = keys[index];
          item.value = editData[keys[index]] || false;
          return item;
        });
      case 'airFields':
        keys = ['airDateFields','airPassengerFields','airCityFields','airCabinFields'];
        return beforeOptionsAir.map((item, index) => {
          item.keyName = keys[index];
          item.value = editData[keys[index]] || false;
          return item;
        });
      case 'hotelFields':
        keys = ['hotelDateFields','hotelCheckInFields','hotelCityFields'];
        return beforeOptionsHotel.map((item, index) => {
          item.keyName = keys[index];
          item.value = editData[keys[index]] || false;
          return item;
        });
    }
  },

  getCtripForm(getFieldDecorator, title, type, editData){
    let options = this.getCtripKeys(type, editData);
    return (
      <div className='new-item-box'>
        <div className='new-item-label-style'>
          <span className='form-label-text-style'>{title}</span>
        </div>
        <div className='new-item-value-style'>
          {
            options.map(item => {
              return (
                <FormItem key={item.keyName} style={{display:'inline-block'}}>
                  {
                    getFieldDecorator(item.keyName,{
                      initialValue: item.value,
                      valuePropName:'checked',
                    })(
                      <Checkbox>{item.label}</Checkbox>
                    )
                  }
                </FormItem>
              )
            })
          }
        </div>
      </div>
    )
  },

  getTitleComponent(title, marginTop){
    return (
      <h3 style={{borderBottom: 0}}>
        <div className='other-rule-edit-title' style={{marginTop}}>
          {title}
        </div>
      </h3>
    )
  },

  getAdvanceTable(context) {
    const {advanceTableServer, advanceTableRef, advanceTableOnGetData, state, props, onTableRow, exportOperator} = context;
    const {columns, searchForm} = state;
    const { EditFrame } = props;
    const option = {
      service: advanceTableServer,
      index: true,
      onRef: advanceTableRef,
      rowKey: (record,index) => record.companyCode || index,
      onGetData: advanceTableOnGetData,
      columns: columns,
      searchForm: searchForm,
      onRow: (record, index) => ({
        onClick: () => onTableRow(record, EditFrame, index)
      }),
      bordered: true,
    }
    return (
      <AdvanceTable.SimpleTable key='consumer-open' {...option}>
        {exportOperator()}
      </AdvanceTable.SimpleTable>
    )
  },

  /**
   * 详情 tab页【应用设置】
   * 适用ConsumerOpenDetail通用配置如下，如果自定义组件如CMBCOpenDetail无视下列步骤。
   * 1.switchLoadComponentDetail中添加新开通消费商key.
   * 2.consumerConfig中配置搜所，服务以及侧滑内容
   * 3.ConsumerColumns.getNewOpenConsumerColumns 中设置表格列
   * @param context
   * @return {*}
   */
  switchLoadComponentDetail(context) {
    const {type} = context.props.params;
    const {cmbcOpenStatus} = context.state;
    const {getCMBCStatus} = context;
    switch (type) {
      case 'CMBC':
        return (<CMBCOpenDetail openStatus={cmbcOpenStatus} onOpen={getCMBCStatus}/>);
      case 'meiya':
      case 'ctrip':
      case 'yget':
      case 'didi':
      case 'zhenxuanair':
      case 'heliosHotel':
      case 'travelnoone':
        return (<ConsumerOpenDetail {...this.getOpenDetailParams(type, 'openDetail')}/>);
      case 'myacard':
      case 'rebeccard':
        return (<CardOpenDetail type={type} />);
      case 'AntCreditPay':
        return (<AlipayOpenDetail type={type} />);
      case 'GaodeEnterpriseCar':
        return (<GaodeOpenDetail/>)
      default:
        return {};
    }
  },

  /**
   * 设置 tab页【信息同步】
   * @param context
   * @return {*}
   */
  switchLoadComponentOther(context) {
    const {type} = context.props.params;
    switch (type) {
      case 'CMBC':
        return (<CMBCSetting/>);
      case 'meiya':
      case 'ctrip':
      case 'didi':
      // case 'travelnoone':
        return (<ConsumerOtherSetting {...this.getOpenDetailParams(type, 'otherSetting')}/>);
      case 'yget':
        return (<YgetQutoAdjust/>)
      default:
        return false; // 不显示信息同步页
    }
  },

  getOpenDetailParams(type, attr) {
    if (this.consumerConfig[type]) {
      return {
        type,
        ...this.consumerConfig[type][attr]
      }
    } else {
      return {};
    }
  },

  // 个供应商开通详情组件属性配置项，如没有配置属性，也需要注册一个空对象如CMBC(用于显示是否展示在线开通按钮)
  consumerConfig: {
    meiya: {
      openDetail: {
        searchForm: ConsumerUtil2.newOpenConsumerSearchForm('meiya'),
        service: 'getMeiyaList',
        EditFrame: MeiyaEditFrame
      },
      otherSetting: {}
    },
    ctrip: {
      openDetail: {
        searchForm: ConsumerUtil2.newOpenConsumerSearchForm('ctrip'),
        service: 'getCtripList',
        EditFrame: CtripEditFrame
      },
      otherSetting: {
        // employeeSynContent: context => SupplierUtil.getOtherEmployeeSetting(context, 'ctrip'),
        // ctripSetting: (<CtripSetting/>),
      }
    },
    didi: {
      openDetail: {
        searchForm: ConsumerUtil2.newOpenConsumerSearchForm('didi'),
        service: 'getDidiList',
        EditFrame: DidiEditFrame
      },
      otherSetting: {
        depSynContent: context => ConsumerUtil2.getOtherDepSetting(context, 'didi'),
        didiSettingContent: context => ConsumerUtil2.getDidiSetting(context, 'didi')
      }
    },
    yget: {
      openDetail: {
        noExport: true,
        searchForm: ConsumerUtil2.newOpenConsumerSearchForm(),
        service: 'getYgetList',
        EditFrame: YgetEditFrame
      },
      otherSetting: {}
    },
    // heliosHotel: {
    //   openDetail: {
    //     searchForm: ConsumerUtil2.newOpenConsumerSearchForm('heliosHotel'),
    //     service: 'getMeiyaList',
    //     EditFrame: HeliosHotelFrame
    //   },
    //   otherSetting: {}
    // },
    // zhenxuanair: {
    //   openDetail: {
    //     searchForm: ConsumerUtil2.newOpenConsumerSearchForm('zhenxuanair'),
    //     service: 'getMeiyaList',
    //     EditFrame: HeliosPlaneFrame
    //   },
    //   otherSetting: {}
    // },
    CMBC: {},
    myacard: {},
    rebeccard: {},
    AntCreditPay: {},
    GaodeEnterpriseCar: {},
    travelnoone:{
      openDetail: {
      searchForm: ConsumerUtil2.newOpenConsumerSearchForm('travelnoone'),
      service: 'getTravelnooneList',
      EditFrame: TravelNoOneEditFrame
    }
  //   otherSetting: {
  //     depSynContent: context => ConsumerUtil2.getOtherDepSetting(context),
  // }
  }
  },

  getConsumerTravelSetSearchForm(){
    return [
      {
        id: 'levelCode',
        label: messages('supplier-1.key36')/*层级*/,
        component: <Radio.Group>
          <Radio value="ALL">{messages('common.all')/*全部*/}</Radio>
          <Radio value="COMPANY">{messages('supplier-1.key37')/*公司*/}</Radio>
          <Radio value="TENANT">{messages('supplier-1.key38')/*租户*/}</Radio>
        </Radio.Group>,
        initialValue: 'ALL',
        dataIndex: 'levelCode',
        parser: value => ({
          levelCode: value !== 'ALL' ? value : null
        }),
        tagParser: value => {
          if (value === 'ALL') {
            return messages('common.all')/*全部*/;
          } else {
            return value === "COMPANY" ? messages('supplier-1.key37')/*公司*/ : messages('supplier-1.key98')/*帐套*/;
          }
        }
      },
      {
        component: 'Chooser',
        id: 'distLevelOrgName',
        dataIndex: 'distLevelOrgName',
        label: messages('supplier-1.key99')/*启用公司*/,
        props: {
          type: 'permission_company',
          valueKey: 'id',
          labelKey: 'name',
          single: false,
          onlyNeed: 'id'
        },
        parser: value => ({
          distLevelOrgIds: value && value.length > 0 ? value : [],
        }),
      },
    ]
  },

  exportErrorColumns(){
    return [
      {title:messages('supplier-1.key100')/*行号*/,dataIndex:'rowNum',key:'rowNum',width: '13%'},
      {title:messages('supplier-1.key33')/*公司名称*/,dataIndex:'companyName',key:'companyName',width: '25%'},
      {title:messages('supplier-1.key101')/*错误信息*/,dataIndex:'error',key:'error'},
    ]
  },

}
